package net.togogo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class getParameterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("getParameterServlet---->doPOST方法被调用");
        //设置请求体的字符集为UT-8 必须卸载参数调用之前
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String[] hobbies = request.getParameterValues("hobby");
        System.out.println("name = " + name);
        System.out.println("password = " + password);
        System.out.println("hobbies = " + Arrays.asList(hobbies));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("getParameterServlet---->doGet方法被调用");

        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String[] hobbies = request.getParameterValues("hobby");
        //get请求方式乱码解决 先以ISO-8859-1进行编码 然后使用UTF-8进行解码
        //name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
        System.out.println("name = " + name);
        System.out.println("password = " + password);
        System.out.println("hobbies = " + Arrays.asList(hobbies));
    }
}
